<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="团队名称" prop="teName">
        <el-input
          v-model="queryParams.teName"
          placeholder="请输入团队名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="代理商" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入代理商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:aTeam:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:aTeam:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:aTeam:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="aTeamList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="团队名称" align="center" prop="teName" />
      <el-table-column label="代理商" align="center" prop="nickName" />
      <el-table-column label="团队人数" align="center" prop="teNum" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
<!--      <el-table-column label="是否上线" align="center" prop="isShow" >-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isShow"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="排序" align="center" prop="numSoft" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:aTeam:edit']"
          >修改</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-plus"-->
<!--            @click="handleAddUser(scope.row)"-->
<!--          >增加团员</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleQue(scope.row)"
          >查看团员</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:aTeam:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改团队对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="团队名称" prop="teName">
          <el-input v-model="form.teName" placeholder="请输入团队名称" />
        </el-form-item>
<!--        <el-form-item label="团队人数" prop="teNum">-->
<!--          <el-input v-model="form.teNum" placeholder="请输入团队人数" />-->
<!--        </el-form-item>-->
        <el-form-item label="选择代理" prop="userId">
          <el-select v-model="form.userId"  placeholder="请选择代理">
            <el-option
              v-for="item in aUserList"
              :key="item.id"
              :label="item.nickName"
              :value="parseInt(item.id)"
            ></el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="是否上线" prop="isShow">-->
<!--          <el-select v-model="form.isShow" placeholder="请选择是否上线">-->
<!--            <el-option-->
<!--              v-for="dict in dict.type.sys_yes_no"-->
<!--              :key="dict.value"-->
<!--              :label="dict.label"-->
<!--              :value="dict.value"-->
<!--            ></el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="排序" prop="numSoft">-->
<!--          <el-input-number v-model="form.numSoft" :min="0"  placeholder="请输入排序" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listATeam, getATeam, delATeam, addATeam, updateATeam } from "@/api/system/aTeam";
import { listAUser } from "@/api/system/aUser";

export default {
  name: "ATeam",
  dicts: ['sys_yes_no'],
  data() {
    return {
      falseUserId:"",//伪id--前用户ID
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 团队表格数据
      aTeamList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teName: null,
        teNum: null,
        isShow: null,
        numSoft: null,
        nickName: null,
        phone: null,
      },
      // 表单参数
      form: {
        qrCode:"",
      },
      // 表单校验
      rules: {
        teName: [
          { required: true, message: "团队名称不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户名称不能为空", trigger: "blur" }
        ],
        numSoft: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ],
        isShow: [
          { required: true, message: "是否上线不能为空", trigger: "change" }
        ]
      },
      //下拉用户数据
      aUserList:[],
    };
  },
  created() {
    this.getList();
    this.getUserList();
  },
  methods: {
    getUserList(){
      var query = {
        pageNum: 1,
        pageSize: 1000,
        role:2,
      }
      listAUser(query).then(response => {
        this.aUserList = response.rows;
      });
    },
    handleAddUser(row){
      this.$router.push({
        path:"/userIndex",
        query:{
          teamId:row.id
        }
      })
    },
    handleQue(row){
      this.$router.push({
        path:"/aTeamInfo",
        query:{
          teamId:row.id
        }
      })
    },
    /** 查询团队列表 */
    getList() {
      this.loading = true;
      listATeam(this.queryParams).then(response => {
        this.aTeamList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        teName: null,
        teNum: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        delFlag: null,
        isShow: null,
        numSoft: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加团队";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getATeam(id).then(response => {
        this.form = response.data;
        this.falseUserId = this.form.userId;
        this.form.userId = this.form.nickName;
        this.open = true;
        this.title = "修改团队";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            if(this.form.nickName == this.form.userId){
              this.form.userId = this.falseUserId;
            }else{
              this.form.falseUserId = this.falseUserId;
            }
            updateATeam(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.getUserList();
            });
          } else {
            addATeam(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.getUserList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('删除团队,会删除所有团员信息！').then(function() {
        return delATeam(ids);
      }).then(() => {
        this.getList();
        this.getUserList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/aTeam/export', {
        ...this.queryParams
      }, `aTeam_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
